<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Controller as BaseController;
use Illuminate\Support\Facades\Response;
use Validator;
use Session;
use App\Http\Models\ArticleTypeModel;
use App\Http\Models\CategoryModel;
use App\Http\Models\ArticleModel;

class CategoryController extends Controller
{
    public function index()
    {
        //

     /*   $data['data'] = ArticleTypeModel::paginate(10);
        $data['type'] = '3';
        dd($data);*/

        $data['data'] = CategoryModel::where('parent_id',0)->paginate(10);
        $data['type'] = 3;
        return view('category.list',compact('data'));
    }


    public function add(Request $request)
    {
        if($request->isMethod('get')){
            $data=CategoryModel::paginate(100);
            return  view('category.add',compact('data')) ;
        }else{
            $this->validate($request,[
                'name'=>'required|string|max:100|min:2',
            ]);


            $data = $request->except(['_token','uploadfile']);
            CategoryModel::create($data);
           // $data['data'] = CategoryModel::paginate(10);
            return redirect('/admin/category');
        }

    }

    public function edit(Request $request ,$id)
    {
        if ($request->isMethod('get')){

            $data = CategoryModel::where(['id' => $id])->first();
            $category=CategoryModel::paginate(100);
            return view('category.edit', compact('data','category'));
        }else{
            $data = $request->except(['_token','uploadfile']);
            $validator = Validator::make($data, [
                'name'     => 'required|string',
            ]);
            if ($validator->fails()) {
                return redirect('/admin/category/edit/'.$id)
                    ->withErrors($validator)
                    ->withInput($data);
            }
            CategoryModel::where(['id'=>$id])->update($data);
            return redirect('/admin/category');
        }
    }

    public function del_category(Request $request )
    {

        $id = $request->input('id');
        $res = CategoryModel::where(['id'=>$id])->first();
        if(!empty($res)) {
            CategoryModel::where(['id'=>$id])->delete();
            return response()->json(['status' => 200, 'msg' =>'删除成功']);
        }
        return response()->json(['status' => 500, 'msg' =>'删除失败']);
    }


    //学院管理
    public function collegelist(Request $request,$id)
    {
        $data['data'] = CategoryModel::where('parent_id',$id)->paginate(10);
        $data['type'] = 3;
        return view('category.collegelist',compact('data'))->with('id',$id);
    }

    public function collegeadd(Request $request,$id)
    {
        if($request->isMethod('get')){
            return  view('category.addcollege')->with('id',$id) ;
        }else{
            $this->validate($request,[
                'name'=>'required|string|max:100|min:2',
            ]);
            $data = $request->except(['_token','uploadfile']);
            $data['parent_id']=$id;

            CategoryModel::create($data);
            return redirect('/admin/collegelist/'.$id);
        }
    }

    public function collegeedit(Request $request ,$id,$s_id)
    {
        if ($request->isMethod('get')){

            $data = CategoryModel::where(['id' => $id])->first();
            return view('category.editcollege', compact('data'))->with('s_id',$s_id);
        }else{
            $data = $request->except(['_token','uploadfile']);
            $validator = Validator::make($data, [
                'name'     => 'required|string',
            ]);
            if ($validator->fails()) {
                return redirect('/admin/collegeedit/'.$id.'/school/'.$s_id)
                    ->withErrors($validator)
                    ->withInput($data);
            }
            CategoryModel::where(['id'=>$id])->update($data);
            return redirect('/admin/collegelist/'.$s_id);
        }

    }


    public function get_school(){
        $data = CategoryModel::where('parent_id',0)->get();
        return response()->json(['status'=>200,'msg'=> $data ]);
    }







}
